package lg.minecraft.plugin.entityclear.template;

import lg.minecraft.plugin.entityclear.component.EntityFilter;
import lg.minecraft.plugin.entityclear.component.EntityRemover;
import lg.minecraft.plugin.entityclear.component.WorldFilter;
import lg.minecraft.plugin.entityclear.task.EntityClearTask;
import lombok.Getter;
import org.bukkit.World;
import org.bukkit.entity.Entity;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.HashMap;
import java.util.Map;

/**
 * @author langle__
 * @version 1.0
 */
@Getter
public class EntityClearTaskTemplate {

    private final int clearInterval;

    @NotNull private final Map<Integer, String> messages;
    @Nullable private final String clearMessage;
    @NotNull private final WorldFilter worldFilter;
    @NotNull private final EntityFilter entityFilter;
    @NotNull private final EntityRemover entityRemover;

    protected EntityClearTaskTemplate(Builder builder) {
        this.clearInterval = builder.clearInterval;
        this.messages = builder.messages;
        this.clearMessage = builder.clearMessage;
        this.worldFilter = builder.worldFilter;
        this.entityFilter = builder.entityFilter;
        this.entityRemover = builder.entityRemover;
    }

    public EntityClearTask createTask() {
        return new EntityClearTask(this);
    }

    public Map<Integer, String> getMessages() {
        return new HashMap<>(messages);
    }

    /**
     * 测试世界是否符合过滤条件
     *
     * @param world 待测试的世界对象
     * @return 如果符合过滤条件返回true，否则返回false
     */
    public boolean testWorld(World world) {
        return worldFilter.test(world);
    }

    /**
     * 测试实体是否符合过滤条件
     *
     * @param entity 待测试的实体对象
     * @return 如果符合过滤条件返回true，否则返回false
     */
    public boolean testEntity(Entity entity) {
        return entityFilter.test(entity);
    }

    /**
     * 测试实体删除器是否可以删除指定实体
     *
     * @param entity 待删除的实体对象
     * @return 如果实体可以被删除返回true，否则返回false
     */
    public boolean entityRemover(Entity entity) {
        try {
            return entityRemover.remove(entity);
        } catch (Exception e) {
            return false;
        }
    }

    public static class Builder {
        private int clearInterval = 0;
        private Map<Integer, String> messages = new HashMap<>();
        private WorldFilter worldFilter;
        private EntityFilter entityFilter;
        private EntityRemover entityRemover;
        private String clearMessage;

        public Builder clearInterval(int clearInterval) {
            this.clearInterval = clearInterval;
            return this;
        }

        public Builder messages(@NotNull Map<Integer, String> messages) {
            this.messages = new HashMap<>(messages);
            return this;
        }

        public Builder addMessage(int time, String message) {
            this.messages.put(time, message);
            return this;
        }

        public Builder worldFilter(WorldFilter worldFilter) {
            this.worldFilter = worldFilter;
            return this;
        }

        public Builder entityFilter(EntityFilter entityFilter) {
            this.entityFilter = entityFilter;
            return this;
        }

        public Builder entityRemover(EntityRemover entityRemover) {
            this.entityRemover = entityRemover;
            return this;
        }

        public Builder clearMessage(String clearMessage) {
            this.clearMessage = clearMessage;
            return this;
        }

        public EntityClearTaskTemplate build() {
            if (worldFilter == null || entityFilter == null || entityRemover == null) {
                throw new IllegalStateException("WorldFilter, EntityFilter and EntityRemover must be set");
            }
            return new EntityClearTaskTemplate(this);
        }

    }

}
